package com.example.namnh_games_button;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends Activity {

	Timer t;  
	TimerTask task; 
	
	ArrayList<CustomButton> btnList = new ArrayList<CustomButton>();
	TableLayout tableLayout;
	
	int tmp = 0;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tableLayout = (TableLayout) findViewById(R.id.TableLayout1);
        
        TableRow tr1 = new TableRow(this);
        tr1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        
        TableRow tr2 = new TableRow(this);
        tr2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        
        TableRow tr3 = new TableRow(this);
        tr3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        
        for (int i = 0; i < 9; i++) {
        	CustomButton btn = new CustomButton(getBaseContext(), i, 0);
        	btn.setState(0);
        	btn.setText("" + btn.getState());
        	btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					CustomButton btn = (CustomButton) v;
					
					switch (btn.getId())  {
					case 0:
						if (btn.getState() == 1) {
							btn.setState(2);
							btn.setText("2");
						}
						break;
					case 1:
						if (btn.getState() == 1) {
							btn.setState(2);
							btn.setText("2");
						}
						break;
					case 2:
						if (btn.getState() == 1) {
							btn.setState(2);
							btn.setText("2");
						}
						break;
					case 3:
						if (btn.getState() == 1) {
							btn.setState(2);
							btn.setText("2");
						}
						break;	
					case 4:
						if (btn.getState() == 1) {
							btn.setState(2);
							btn.setText("2");
						}
						break;
					case 5:
						if (btn.getState() == 1) {
							btn.setState(2);
							btn.setText("2");
						}
						break;
					case 6:
						if (btn.getState() == 1) {
							btn.setState(2);
							btn.setText("2");
						}
						break;
					case 7:
						if (btn.getState() == 1) {
							btn.setState(2);
							btn.setText("2");
						}
						break;	
					case 8:
						if (btn.getState() == 1) {
							btn.setState(2);
							btn.setText("2");
						}
						break;						
					default:
						break;
					}
				}
			});
        	
        	btnList.add(btn);
        }
        
        for (CustomButton btn: btnList) {
        	if (btn.getId() < 3) {
        		tr1.addView(btn);
        	} else if (btn.getId() < 6) {
        		tr2.addView(btn);
        	} else {
        		tr3.addView(btn);
        	}
        }
        
        tableLayout.addView(tr1, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
        tableLayout.addView(tr2, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
        tableLayout.addView(tr3, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

        startTimer();
        
    }

    @Override
	protected void onResume() {
		super.onResume();
	}
    
    public void startTimer(){  
    	t = new Timer();     
    	task = new TimerTask() {
    		@Override  
    		public void run() {  
    			runOnUiThread(new Runnable() {  
    	  
    				@Override  
    	    		public void run() {  
    					for (CustomButton btn : btnList) {
    						if (btn.getId() == tmp) {
    							btn.setState(1);
    							btn.setText("1");    							
    						}
    					}
    					tmp += 1;
    	    		}  
    			});  
    		}  
    	};  
    	t.scheduleAtFixedRate(task, 0, 1000);  
	}  
    
    public void runEachSec() {
//    	final ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
//
//		exec.schedule(new Runnable(){
//		    @Override
//		    public void run(){
//		        Log.i("DEBUG", "Hello");
//		    }
//		}, 1, TimeUnit.SECONDS);
		
		final Timer timer = new Timer();

		timer.schedule(new TimerTask() {
		    public void run() {
		          
		          Log.w("General", "This has been called one second later");
		        timer.cancel();
		    }
		}, 1000);
    }
    
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
